package com.cqut.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cqut.pojo.vo.noticeInfoVO;
import com.cqut.service.INoticeManagerService;
import com.cqut.model.NoticeManager;
import com.cqut.dao.NoticeManagerMapper;
import com.cqut.dao.NoticeManagerMapperExtra;
import com.cqut.dto.LimitShowDTO;
import com.cqut.dto.NoticeManagerDTO;
import com.cqut.util.SqlUtil;

@Service
public class NoticeManagerService implements INoticeManagerService {
    @Resource(name="noticeManagerMapper")
    private NoticeManagerMapper noticeManagerMapper;

    @Resource(name="noticeManagerMapperExtra")
    private NoticeManagerMapperExtra noticeManagerMapperExtra;
    
    @Override
    public boolean save(NoticeManager noticeManager) {
    	String noticeId = String.valueOf(System.currentTimeMillis());
    	noticeManager.setNoticeId(noticeId);
        return noticeManagerMapper.insert(noticeManager) > 0;
    }

    @Override
    public boolean update(NoticeManager noticeManager) {
        return noticeManagerMapper.updateByPrimaryKeySelective(noticeManager) >= 0;
    }

    @Override
    public boolean delete(String noticeId) {
        return noticeManagerMapper.deleteByPrimaryKey(noticeId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return noticeManagerMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public NoticeManager findModel(String noticeId) {
        return noticeManagerMapper.selectByPrimaryKey(noticeId);
    }

    @Override
    public NoticeManager findModel(String noticeId, String[] parameters) {
        return noticeManagerMapper.findModel(noticeId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String noticeId) {
        String[][] parameters = {{"notice_id", "noticeId"}, {"notice_title", "noticeTitle"}, {"publish_time", "publishTime"}, {"publish_id", "publishId"}, {"publish_name", "publishName"}, {"notice_detaile", "noticeDetaile"}, {"notice_type", "noticeType"}, {"file_id", "fileId"}, {"page_view", "pageView"}};
        return this.findMap(noticeId, parameters);
    }

    public Map<String, Object> findMap(String noticeId, String[][] parameters) {
        return noticeManagerMapper.findMap(noticeId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<NoticeManager> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<NoticeManager> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<NoticeManager> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return noticeManagerMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"notice_id", "noticeId"}, {"notice_title", "noticeTitle"}, {"publish_time", "publishTime"}, {"publish_id", "publishId"}, {"publish_name", "publishName"}, {"notice_detaile", "noticeDetaile"}, {"notice_type", "noticeType"}, {"file_id", "fileId"}, {"page_view", "pageView"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"notice_id", "noticeId"}, {"notice_title", "noticeTitle"}, {"publish_time", "publishTime"}, {"publish_id", "publishId"}, {"publish_name", "publishName"}, {"notice_detaile", "noticeDetaile"}, {"notice_type", "noticeType"}, {"file_id", "fileId"}, {"page_view", "pageView"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return noticeManagerMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return noticeManagerMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }
    
    @Override
    public List<noticeInfoVO> loadNoticeList(NoticeManagerDTO noticeManagerDTO,LimitShowDTO limitShowDTO) {
    	return noticeManagerMapperExtra.findNotices(noticeManagerDTO.getNoticeType(),noticeManagerDTO.getNoticeTitle(),limitShowDTO.calculateStart(), limitShowDTO.calculateEnd());
    }
    
    @Override
    public List<noticeInfoVO> loadNoticeListStates(NoticeManagerDTO noticeManagerDTO,LimitShowDTO limitShowDTO) {
    	return noticeManagerMapperExtra.findNoticesStates(noticeManagerDTO.getNoticeType(),noticeManagerDTO.getNoticeTitle(),limitShowDTO.calculateStart(), limitShowDTO.calculateEnd());
    }

	@Override
	public Object findCountOfNotices(NoticeManagerDTO noticeManagerDTO) {
		String count = 
				noticeManagerMapperExtra.findNoticesNo(noticeManagerDTO.getNoticeType(),noticeManagerDTO.getNoticeTitle(), -1, -1);
		return count;
	}
	
	@Override
	public Object findCountOfNoticesStates(NoticeManagerDTO noticeManagerDTO) {
		String count = 
				noticeManagerMapperExtra.findNoticesStatesNo(noticeManagerDTO.getNoticeType(),noticeManagerDTO.getNoticeTitle(), -1, -1);
		return count;
	}

	@Override
	public Map<String, Object> getNoticeById(String noticeId) {
		return noticeManagerMapperExtra.getNoticeById(noticeId);
	}
	/**
	 * 获得主办方信息
	 */
	@Override
	public Map<String, Object> getOnwerById(String noticeId) {
		return noticeManagerMapperExtra.getOnwerById(noticeId);
	}

	@Override
	public List<Map<String, Object>> getCompetition(String userId) {
		// TODO Auto-generated method stub
		return noticeManagerMapperExtra.getCompetition(userId);
	}
	
	/**
	 * 根据当前用户ID获得所关注的大赛的动态消息
	 */
	@Override
	public List<Map<String, Object>> loadNoticeStatesUser(String userId,LimitShowDTO limitShowDTO) {
		// TODO Auto-generated method stub
		return noticeManagerMapperExtra.getNoticeStateUser(userId,limitShowDTO.calculateStart(), limitShowDTO.calculateEnd());
	}
	
	
	/**
	 * 根据当前用户ID获得所关注大赛的动态消息的数量
	 */
	@Override
	public Object countForNoticesStatesUser(String userId) {
		String count = 
				noticeManagerMapperExtra.findNoticesStatesUserNo(userId, -1, -1);
		return count;
	}

}
